/**
 * @Author : Arthur
 * @Date : create on 2018-9-18
 * @Time : 13:46
 */
var demandExplain;
var technologyExplain;
var href;
var acceptUpload = ['doc', 'docx', 'xls', 'xlsx', 'txt', 'ppt', 'pptx', 'sql', 'jpg', 'png', 'jpge'];

$(function () {
    demandExplain = UM.getEditor('demandExplain', {
        textarea: 'demandExplain',
        initialFrameHeight: 220,
        initialContent: ''
    });
    technologyExplain = UM.getEditor('technologyExplain', {
        textarea: 'technologyExplain',
        initialFrameHeight: 220,
        initialContent: ''
    });
    loadCombTree();
});

function createFileInput(num) {
    var markNum = 0;
    if (0 === num) {
        markNum = $('#demandExplainFile_div table tbody tr').length;
        $('#demandExplainFile_div table tbody').append(
            '<tr>' +
            ' <td>' +
            '  <input type="file" name="demandExplainFile[' + markNum + ']" style="width: 100%;"/>' +
            ' </td>' +
            ' <td onclick="this.children[0].click()">' +
            '  <i class="fa fa-minus" aria-hidden="true" style="margin: 5px 10px" onclick="deleteThisLine(this)"></i>' +
            ' </td>' +
            '</tr>'
        );
    } else if (1 === num) {
        markNum = $('#technologyExplainFile_div table tbody tr').length;
        $('#technologyExplainFile_div table tbody').append(
            '<tr>' +
            ' <td>' +
            '  <input type="file" name="technologyExplainFile[' + markNum + ']" style="width: 100%;"/>' +
            ' </td>' +
            ' <td onclick="this.children[0].click()">' +
            '  <i class="fa fa-minus" aria-hidden="true" style="margin: 5px 10px" onclick="deleteThisLine(this)"></i>' +
            ' </td>' +
            '</tr>'
        );
    } else if (3 === num) {
        markNum = $('#demandExplainFile_div table tbody tr').length;
        $('#demandExplainFile_div table tbody').append(
            '<tr>' +
            ' <td>' +
            '  <a href="" target="_blank"></a>' +
            '  <input type="hidden" name="demandExplainFile[' + markNum + ']" style="width: 100%;" readonly/>' +
            '  <input type="hidden" name="demandExplainFilePath[' + markNum + ']" style="width: 100%;" readonly/>' +
            ' </td>' +
            ' <td onclick="this.children[0].click()">' +
            '  <i class="fa fa-minus" aria-hidden="true" style="margin: 5px 10px" onclick="deleteThisLine(this)"></i>' +
            ' </td>' +
            '</tr>'
        );
    } else if (4 === num) {
        markNum = $('#technologyExplainFile_div table tbody tr').length;
        $('#technologyExplainFile_div table tbody').append(
            '<tr>' +
            ' <td>' +
            '  <a href="" target="_blank"></a>' +
            '  <input type="hidden" name="technologyExplainFile[' + markNum + ']" style="width: 100%;" readonly/>' +
            '  <input type="hidden" name="technologyExplainFilePath[' + markNum + ']" style="width: 100%;" readonly/>' +
            ' </td>' +
            ' <td onclick="this.children[0].click()">' +
            '  <i class="fa fa-minus" aria-hidden="true" style="margin: 5px 10px" onclick="deleteThisLine(this)"></i>' +
            ' </td>' +
            '</tr>'
        );
    }
}

function deleteThisLine(dom) {
    $(dom).parent().parent().remove();
}

function loadCombTree() {
    $('#funcId').combotree({
        url: '../../manage/modify/getFunc.do',
        editable: false,
        multiple: false,
        required: true,
        lines: true,
        animate: true,
        onlyLeafCheck: true,
        onBeforeSelect: function (node) {
            return '1' === node.attribute.openType;
        },
        onSelect: function (node) {
            $('#gnMc').val(node.attribute.gnMc);
            $('#funcHref').val(node.attribute.href);
            $('#funcExplain').val(node.attribute.gnms);
            $.ajax({
                url: "../../manage/modify/getFuncDetail.do",
                data: {
                    reqData: {
                        funcId: node.attribute.gnId
                    }
                },
                type: "post",
                timeout: sys_timeout,
                dataType: "json"
            }).fail(function (XMLHttpRequest, textStatus) {
                innerToast.error(textStatus);
            }).done(function (responseText) {
                if (checkResponse(responseText)) {
                    if (isNotNull(responseText.repData.jrMap.xqsm))
                        demandExplain.setContent(responseText.repData.jrMap.xqsm);
                    if (isNotNull(responseText.repData.jrMap.jssm))
                        technologyExplain.setContent(responseText.repData.jrMap.jssm);
                    var xqList = responseText.repData.xqList;

                    $('#demandExplainFile_div table tbody').empty();
                    $('#technologyExplainFile_div table tbody').empty();

                    if (isNotNull(xqList)) {
                        xqList.forEach(function (value) {
                            createFileInput(3);
                            $('#demandExplainFile_div table tbody tr:last a').text(value.fjmc).attr('href', download_server_url + value.fjlj + '?attachName=' + value.fjmc);
                            $('#demandExplainFile_div table tbody tr:last input:first').val(value.fjmc);
                            $('#demandExplainFile_div table tbody tr:last input:last').val(value.fjlj);
                        });
                    }
                    var jsList = responseText.repData.jsList;
                    if (isNotNull(jsList)) {
                        jsList.forEach(function (value) {
                            createFileInput(4);
                            $('#technologyExplainFile_div table tbody tr:last a').text(value.fjmc).attr('href', download_server_url + value.fjlj + '?attachName=' + value.fjmc);
                            $('#technologyExplainFile_div table tbody tr:last input:first').val(value.fjmc);
                            $('#technologyExplainFile_div table tbody tr:last input:last').val(value.fjlj);
                        });
                    }
                } else {
                    innerToast.warn(responseText.msg);
                }
            });
        }
    });
}

function subForm() {
    if (isNull($('#funcId').val())) {
        innerToast.info('请选择功能');
        $('#funcId').focus();
        return;
    }
    if (isNull($('#gnMc').val())) {
        innerToast.info('请填写功能名称');
        $('#gnMc').focus();
        return;
    }
    if (isNull($('#funcExplain').val())) {
        innerToast.info('请填写功能描述');
        $('#funcExplain').focus();
        return;
    }
    if (!demandExplain.hasContents()) {
        innerToast.info('请填写需求说明');
        demandExplain.focus();
        return;
    }
    if (!technologyExplain.hasContents()) {
        innerToast.info('请填写技术说明');
        technologyExplain.focus();
        return;
    }
    var flag = false;
    var files = $('input[type=file]');
    var $file;
    for (var i = 0, len = files.length; i < len; i++) {
        var nameArr = files[i].files[0].name.split('.');
        if (acceptUpload.indexOf(nameArr[(nameArr.length - 1)]) === -1) {
            flag = true;
            $file = $(files[i]);
            break;
        }
    }
    if (flag) {
        innerToast.info('上传文件格式有误');
        $file.focus();
        return;
    }
    flavr.confirm("确定要提交吗", startSubmit);
}

function startSubmit() {
    $.messager.progress({
        title: '提示',
        msg: '保存中，请稍候……',
        text: ''
    });

    $('#demandExplain').text(Base64.encode(demandExplain.getContent()));
    $('#technologyExplain').text(Base64.encode(technologyExplain.getContent()));
    demandExplain.sync();
    technologyExplain.sync();

    $('#modifyForm').form('submit', {
        url: '../../manage/modify/establishModifyApply.do',
        error: function (XMLHttpRequest, textStatus) {
            innerToast.error(textStatus);
        },
        success: function (responseText) {
            responseText = $.parseJSON(responseText);
            $.messager.progress('close');
            if (checkResponse(responseText)) {
                innerToast.success(responseText.msg);
                location.reload();
            } else {
                innerToast.warn(responseText.msg);
            }
        }
    });

    setTimeout(function () {
        $('#demandExplain').html(Base64.decode($('#demandExplain').text()));
        $('#technologyExplain').html(Base64.decode($('#technologyExplain').text()));
    }, 100);
}
